// pages/edit12/edit12.js
Page({

  /**
   * 页面的初始数据
   */
  data: {near_id:'',goods_id:'',files:[]

  },

  onLoad: function (options) {
    if(options.goods_id){
      this.setData({
        goods_id:options.goods_id,
        near_id:options.near_id
      })
    }
      this.setData({
        type:options.type
      })
  },
  getimage1(){
    if(this.data.type==1){
      this.wechattwo()
    }
    if(this.data.type==2){
      this.mobiletwo()
    }

    if(this.data.type==3){
      this.wechatfour()
    }
    if(this.data.type==4){
      this.mobilefour()
    }

  } ,
  wechattwo(){
    let files=this.data.files
    wx.chooseMessageFile({
      
      count: 2,
      success:(res)=>{
        console.log(res);
       files=  files.concat(res.tempFiles)
         this.setData({
           files
         })
         console.log(this.data.files,files);
         
      }
    })
  },
  mobiletwo(){
    let files=this.data.files
    wx.chooseImage({
      sourceType:["album"],
      count: 2,
      success:(res)=>{
        console.log(res);
        files=  files.concat(res.tempFiles)
        this.setData({
          files
        })
      }
    })
  },
  wechatfour(){
    let files=this.data.files
    wx.chooseMessageFile({
      count: 4,
      success:(res)=>{
        console.log(res);
        files=  files.concat(res.tempFiles)
        this.setData({
          files
        })
      }
    })
  },
  mobilefour(){
    let files=this.data.files
    wx.chooseImage({
      count: 4,
      sourceType:["album"],
      success:(res)=>{
        console.log(res);
        files=  files.concat(res.tempFiles)
        this.setData({
          files
        })
      }
    })
  },
  shanchu(e){
    let files=this.data.files
    files.splice(e.currentTarget.dataset.i,1)
    this.setData({
      files,
      
    })
 },
 bianji(e){
  wx.navigateTo({
    url: '/pages/edit5/edit5?url='+this.data.files[e.currentTarget.dataset.i].path+'&flag=14'+'&index3='+e.currentTarget.dataset.i,
  })
},
upload1(){
   if(this.data.files.length==1){
     wx.showToast({
       title: '请上传2张到4张',
       icon:'none'
     })
     return
   }
   wx.showLoading({
     title: '正在上传',
     complete: (res) => {},
     fail: (res) => {},
     mask: true,
     success: (res) => {},
   })
 this.getpdfarr()

},

  // 将多图分别上传 
  getpdfarr(){
    let array=[]
    this.data.files.forEach((item)=>{
      wx.uploadFile({
        filePath: item.path,
        name: 'file',
        url: 'https://zujieba.com/Api/upload/upload',
        success:(res)=>{
          console.log(res);
          let res1=JSON.parse(res.data)
          array.push(res1.name)
          this.setData({
            array
          })
          if(array.length==this.data.files.length){
            console.log(array);
             if(array.length==2){
              this.getpdfimage(array) 
             }else if(array.length==4){
              this.getpdfimage4(array)
             }else{
              this.getpdfimage3(array)
             }
            
          }
        }
      })
    })
   
   },
   getpdfimage(file){
    wx.request({
      url: 'https://zujieba.com/Api/upload/canvas_four.html',
      data:{type:2,pic_1:file[0],img_1:file[1]},
      method:'POST',
      success:(res)=>{
        console.log(res);
        
        let url="https://zujieba.com/uploads/"+res.data.img
        //将图片转为pdf后需要变为临时路径预览
     
         
          
         
           if(res.data.success==1){
             wx.hideLoading()
           
            wx.navigateTo({
              url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+res.data.img+
              '&time='+res.data.time+'&page=1'+"&url="+url+'&radio5=4'
            })
            // wx.navigateTo({
            //   url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+name+
            //   '&time='+time+'&page=1'+"&url="+url+'&radio5=4'+"&photo=1"+'&param1=1'
            // })
           
           }else{
             wx.showToast({
               title: res.data.tips,
               icon:'none'
             })
           }
           
         
       
      
       
        
      },
      fail:(res)=>{
        console.log(res);
        
      }
    })
  
  },
  getpdfimage4(file){
    wx.request({
      url: 'https://zujieba.com/Api/upload/canvas_four.html',
      data:{type:1,pic_1:file[0],img_1:file[1],pic_2:file[2],img_2:file[3]},
      method:'POST',
      success:(res)=>{
        console.log(res);
        
        let url="https://zujieba.com/uploads/"+res.data.img
        //将图片转为pdf后需要变为临时路径预览
     
         
          
         
           if(res.data.success==1){
             wx.hideLoading()
           
            wx.navigateTo({
              url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+res.data.img+
              '&time='+res.data.time+'&page=1'+"&url="+url+'&radio5=4'
            })
            // wx.navigateTo({
            //   url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+name+
            //   '&time='+time+'&page=1'+"&url="+url+'&radio5=4'+"&photo=1"+'&param1=1'
            // })
           
           }else{
             wx.showToast({
               title: res.data.tips,
               icon:'none'
             })
           }
           
         
       
      
       
        
      },
      fail:(res)=>{
        console.log(res);
        
      }
    })
  
  },
  getpdfimage3(file){
    wx.request({
      url: 'https://zujieba.com/Api/upload/canvas_four.html',
      data:{type:3,pic_1:file[0],img_1:file[1],pic_2:file[2]},
      method:'POST',
      success:(res)=>{
        console.log(res);
        
        let url="https://zujieba.com/uploads/"+res.data.img
        //将图片转为pdf后需要变为临时路径预览
     
         
          
         
           if(res.data.success==1){
             wx.hideLoading()
           
            wx.navigateTo({
              url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+res.data.img+
              '&time='+res.data.time+'&page=1'+"&url="+url+'&radio5=4'
            })
            // wx.navigateTo({
            //   url: '/pages/fileprint/print/newprint?near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&name="+name+
            //   '&time='+time+'&page=1'+"&url="+url+'&radio5=4'+"&photo=1"+'&param1=1'
            // })
           
           }else{
             wx.showToast({
               title: res.data.tips,
               icon:'none'
             })
           }
           
         
       
      
       
        
      },
      fail:(res)=>{
        console.log(res);
        
      }
    })
  
  },

})